package com.yizhilu.os.edu.service.impl.kuaijidao;

import com.yizhilu.os.common.constants.MemConstans;
import com.yizhilu.os.common.contoller.SingletonLoginUtils;
import com.yizhilu.os.common.util.AccessToken;
import com.yizhilu.os.common.util.SystemUtil;
import com.yizhilu.os.common.util.UserAgentUtil;
import com.yizhilu.os.common.util.WeChatUtil;
import com.yizhilu.os.core.entity.PageEntity;
import com.yizhilu.os.core.service.cache.MemCache;
import com.yizhilu.os.core.service.gain.GuidGeneratorService;
import com.yizhilu.os.core.util.DateUtils;
import com.yizhilu.os.core.util.ObjectUtils;
import com.yizhilu.os.core.util.Security.PurseSecurityUtils;
import com.yizhilu.os.core.util.StringUtils;
import com.yizhilu.os.core.util.web.WebUtils;
import com.yizhilu.os.edu.constants.enums.AccountStatus;
import com.yizhilu.os.edu.constants.enums.IntegralKeyword;
import com.yizhilu.os.edu.constants.enums.ReqChanle;
import com.yizhilu.os.edu.constants.enums.UserExpandFrom;
import com.yizhilu.os.edu.dao.user.UserDao;
import com.yizhilu.os.com.yizhilu.os.edu.dao.kuaijidao.UserKuaijidaoDao;
import com.yizhilu.os.edu.dao.user.UserExpandDao;
import com.yizhilu.os.edu.dao.user.UserIntegralDao;
import com.yizhilu.os.edu.entity.user.*;
import com.yizhilu.os.edu.service.card.CardCodeService;
import com.yizhilu.os.edu.service.order.ShopcartService;
import com.yizhilu.os.edu.service.order.TrxorderService;
import com.yizhilu.os.edu.service.user.LoginOnlineService;
import com.yizhilu.os.edu.service.user.UserAccountService;
import com.yizhilu.os.edu.service.user.UserCodeService;
import com.yizhilu.os.edu.service.user.UserExpandService;
import com.yizhilu.os.edu.service.user.UserIntegralService;
import com.yizhilu.os.edu.service.user.UserLoginLogService;
import com.yizhilu.os.edu.service.kuaijidao.UserKuaijiDaoService;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yizhilu.os.edu.entity.kuaijidao.TemplateData;
import com.yizhilu.os.edu.entity.kuaijidao.WeixinBind;
import com.yizhilu.os.edu.entity.kuaijidao.WeixinSendMsg;
import com.yizhilu.os.edu.entity.kuaijidao.WxTemplate;
import java.sql.Timestamp;
import com.yizhilu.os.edu.entity.kuaijidao.WeixinTempMsgLog;

@Service("UserKuaijiDaoService")
public class UserKuaijiDaoServiceImpl  implements UserKuaijiDaoService
{
  @Autowired 
  private ShopcartService shopcartService;
  @Autowired
  UserIntegralDao userIntegralDao;
  private static final int HMAC_KEY_LEN = 60;
  MemCache memCache = MemCache.getInstance();
  @Autowired
  private UserKuaijidaoDao userKuaijidaoDao;





  public boolean bindUserWeixin(String openId,String userid) {
    WeixinBind weixinBind = new WeixinBind();
    weixinBind.setOpenid(openId);
    weixinBind.setUserid(userid);
    userKuaijidaoDao.addBindWeixin(weixinBind);
    return true;
  }

  public List getUserByOpenId(String openId)
  {
    return userKuaijidaoDao.getUserByOpenId(openId);
  }
  
  /**
   * 获得录播课的列表
   * @param id
   * @return
   */
  public List getCourseKpoint(String id)
  {
    return userKuaijidaoDao.getCourseKpoint(id);
  }

  public void queryGenerateWeixinMsg() throws Exception {
	  
      System.out.println("try create  msg");       
      
      List openIdList=userKuaijidaoDao.getTempMsgOpenId();
      Map<Integer, Integer> kpointIdMap=new HashMap();
      
      if(openIdList!=null && openIdList.size()>0){
    	  
    	  for(int i=0;i<openIdList.size();i++){
    		  
    		  Map temp =(Map)openIdList.get(i);
    		  
    		  String openId =(String)temp.get("openId");
    		  String kpoint_name =(String)temp.get("kpoint_name");
    		  Integer kpoint_id	 =(Integer)temp.get("id");
    		  
    		  kpointIdMap.put(kpoint_id, kpoint_id);
    		    		  
    		  
    		  String begin_time =SystemUtil.timeFormat((Timestamp)temp.get("begin_time"));
	            
		      Map<String,TemplateData> m = new HashMap<String,TemplateData>();
		      TemplateData first = new TemplateData();
		      first.setColor("#000000");  
		      first.setValue("以下课程将于"+begin_time+"开始");  
		      m.put("first", first);  
		       
		      TemplateData name = new TemplateData();  
		      name.setColor("#000000");  
		      name.setValue(kpoint_name);  
		      m.put("keyword1", name);
		      
		      TemplateData wuliu = new TemplateData();  
		      wuliu.setColor("#000000");  
		      wuliu.setValue("全体已购买学员");  
		      m.put("keyword2", wuliu);
		      
		      	   
		      
		      TemplateData remark = new TemplateData();  
		      remark.setColor("#000000");  
		      remark.setValue("***请准时参加***");  
		      m.put("remark", remark);
		     
		      String jsonString =WeChatUtil.getCourseTempMsg(openId,m);
		      
		      WeixinSendMsg weixinSendMsg= new  WeixinSendMsg();
		      
		      weixinSendMsg.setToUser(openId);
		      weixinSendMsg.setMsgType(1);
		      weixinSendMsg.setSendTime(new Date());
		      weixinSendMsg.setIsSend(0);
		      weixinSendMsg.setMsg(jsonString);
		      
		      //添加微信通知消息
		      userKuaijidaoDao.addWeiTempMsg(weixinSendMsg); 
    	  }
    	  
    	  for (Integer key : kpointIdMap.keySet()) {
   		   WeixinTempMsgLog weixinTempMsgLog = new WeixinTempMsgLog();
   		   weixinTempMsgLog.setResourceId(key);
   		   weixinTempMsgLog.setMsgType("1");
   		   weixinTempMsgLog.setSendTime(new Date());
   		   userKuaijidaoDao.addWeiTempMsgLog(weixinTempMsgLog);  
   	     } 
      
      }else{
    	  
    	  System.out.println("今天课程消息都处理完了!");
    	  
      }
    
 
}


public void queryTimingSendWeixinMsg() throws Exception {
    
	queryGenerateWeixinMsg();
	
	//获得队列还没有发送的消息
	List sendMsgList =userKuaijidaoDao.getWeiTempMsgByStatus(0);     
   
     if(sendMsgList!=null && sendMsgList.size()>0){
    	 
	    for(int i=0;i<sendMsgList.size();i++){
	    	
	    	Map m =(Map)sendMsgList.get(i);
			
		    AccessToken at = WeChatUtil.getAccessToken();
		    
		    if (at!=null&&at.getToken()!=null) {
		    	
		      System.out.println("SengMsg:"+m.get("msg").toString());
		      
		      // 调用接口创建菜单
		      int result = WeChatUtil.SendTempMsg(m.get("msg").toString(), at.getToken());
		      
		      
		      Integer id=(Integer)m.get("id");
		      //修改消息的状态
		      userKuaijidaoDao.updateWeiTempMsg(id);
		      // 判断菜单创建结果
		      if (0 == result)
		        System.out.println("发送消息成功");
		      else
		        System.out.println("发送消息失败");
		    }
		     else{
		      System.out.println("发送消息失败");
		    }
		  }
     }else{
    	    System.out.println("当前没有要发送的消息!");
     }
 }

}



/* Location:           D:\licai\kuaijidao\web\WEB-INF\lib\demo_web_v25.jar

 * Qualified Name:     com.yizhilu.os.edu.service.impl.user.UserServiceImpl

 * JD-Core Version:    0.7.0.1

 */